<?php

class Web_MiCarrito extends Web_BasePage
{

    public function mainContent()
    {   
        $carro = $_SESSION['eyoriacart'];
        
        global $db;

        if (isset($carro)) {
            foreach ($carro as $item) {

                $pro = $db->fetchRow($db->select()
                                        ->from('ma_productos', array('pro_nombre', 'pro_key'))
                                        ->where('pro_id=?', $item['item_id']));

                $cart = new Ey_Carrito;
                $subtotal+= $cart->subtotal($cart->verifica_tipo($item['cantidad'], $item['item_price'], null, null, null, null), $item['cantidad'] * $item['item_price']);

                $matriz[] = array('pro_id' => $item['item_id'],
                    'pro_titulo' => $pro->pro_nombre,
                    'pro_key' => $pro->pro_key,
                    'pro_precio' => $item['item_price'],
                    'pro_cantidad' => $item['cantidad'],
                    'pro_sub_total' => $cart->subtotal($cart->verifica_tipo($item['cantidad'], $item['item_price'], null, null, null, null), $item['cantidad'] * $item['item_price'])
                );

            }
        } 
        
        if ($_SESSION['eyoriacart'] == null) {
            unset($_SESSION['idDelivery']);
            unset($_SESSION['precioDelivery']);
        }

        $_SESSION['matriz'] = $matriz;
        $_SESSION['subtotal'] = $subtotal;
        $total = $subtotal + $_SESSION['precioDelivery'];

        $select = $db->select()->from('ma_moneda', array('mon_valor'))->where('mon_key="dolar"');
        $moneda = $db->fetchRow($select);

        $obj = new Web_Db_Delivery();
        $cats = $obj->fetchAll($obj->select()
                                ->order('del_distrito')
                                ->where('del_estado=?', 1));
        $html = '';
        foreach ($cats as $cat) {            
            $html.= '<option id="' . $cat->del_id . '" value="' . round($cat->del_precio * $moneda->mon_valor, 2) . '"';
            $html.= $_SESSION['idDelivery'] == $cat->del_id ? ' selected="selected"' : '';
            $html.= '>' . $cat->del_distrito . '</option>';
        }
        
        if (isset($_SESSION['mensaje'])) {
            $error = $_SESSION['mensaje'];
            unset($_SESSION['mensaje']);
        }
        
        $smarty = new Smarty_Engine();
        $smarty->assign('error', $error);
        $smarty->assign('matriz', $matriz);
        $smarty->assign('subtotal', $subtotal);
        $smarty->assign('total', $total);
        $smarty->assign('html', $html);
        $smarty->assign('precioDelivery', $_SESSION['precioDelivery']);

        return $smarty->fetch(APP_ROOT . DS . 'tpl' . DS . 'mi-carrito.tpl');
    }

}